/*
 * ClienteDao.java
 *
 * Created on 4 de Junho de 2007, 23:19
 *
 */

package net.sf.jOpinionPoll.dao.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import net.sf.jOpinionPoll.dao.ParameterMap;
import net.sf.jOpinionPoll.dao.QuestionarioDao;
import net.sf.jOpinionPoll.entity.Questionario;

/**
 *
 * @author erico
 */
public class QuestionarioDaoJpa extends DaoJpa implements QuestionarioDao {

	/** Creates a new instance of ClienteDao */
    public QuestionarioDaoJpa() {
    }
    
    private DaoJpaHelper<Questionario> getDaoJpaHelper() {
    	return new DaoJpaHelper<Questionario>(getEntityManager(), Questionario.class);
    }

	@Override
	public void add(Questionario newInstance) {
		getDaoJpaHelper().add(newInstance);
	}

	@Override
	public int count() {
		return getDaoJpaHelper().count();
	}

	@Override
	public Questionario findByPk(Object primaryKey) {
		return getDaoJpaHelper().findByPk(primaryKey);
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<Questionario> list(int firstResult, int maxResults) {
		return getDaoJpaHelper().list(firstResult, maxResults);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Questionario> list(int firstResult, int maxResults, ParameterMap paramMap) {
    	EntityManager em = getEntityManager();
    	DaoJpaHelper<Questionario> daoJpaHelper = getDaoJpaHelper();
    	Query query;
    	String sql = 
    		"select q from Questionario q" +
    		" where lower(q.nome) like :nome " + (paramMap.isEmptyValue("nome") ? "or q.nome is null)" : ")") +
    		" order by q.nome";

		query = daoJpaHelper.createQueryHelper(em, sql, paramMap);
        
        if (maxResults > 0)
        	query.setMaxResults(maxResults);
        
        if (firstResult >= 0)
        	query.setFirstResult(firstResult);
        
        return (List<Questionario>)query.getResultList();
	}

	@Override
	public void remove(Questionario questionario) {
		getDaoJpaHelper().remove(questionario);
	}

	@Override
	public void set(Questionario questionario) {
		getDaoJpaHelper().set(questionario);
	}
}